package com.scgl.depot.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.scgl.depot.domain.Depot;
import com.scgl.depot.domain.Location;
import com.scgl.depot.service.IDepotService;
import com.scgl.depot.service.ILocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 库位管理Controller
 *
 * @author mobai
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/location")
public class LocationController extends BaseController
{
    @Autowired
    private ILocationService locationService;
    @Autowired
    private IDepotService depotService;


    /**
     * 查询库位管理列表
     */
    @RequiresPermissions("location:location:list")
    @GetMapping("/list")
    public TableDataInfo list(Location location)
    {
        startPage();
        List<Location> list = locationService.selectLocationList(location);
        return getDataTable(list);
    }

    /**
     * 导出库位管理列表
     */
    @RequiresPermissions("location:location:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Location location)
    {
        List<Location> list = locationService.selectLocationList(location);
        ExcelUtil<Location> util = new ExcelUtil<Location>(Location.class);
        util.exportExcel(response, list, "库位管理数据");
    }

    /**
     * 根据仓库id查询库位管理列表
     */
    @RequiresPermissions("location:location:listByWarehouseId")
    @GetMapping("/listByWarehouseId/{warehouseId}")
    public AjaxResult listByWarehouseId(@PathVariable("warehouseId") Long warehouseId) {
        List<Location> list = locationService.listByWarehouseId(warehouseId);
        return AjaxResult.success("success",list);
    }

    /**
     * 根据仓库列表
     */
    @RequiresPermissions("location:location:getWarehouseList")
    @GetMapping("/getWarehouseList")
    public AjaxResult getWarehouseList() {
        List<Depot> list = depotService.selectDepotList(new Depot());
        return AjaxResult.success("success",list);
    }

    /**
     * 获取库位管理详细信息
     */
    @RequiresPermissions("location:location:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(locationService.selectLocationById(id));
    }

    /**
     * 新增库位管理
     */
    @RequiresPermissions("location:location:add")
    @PostMapping
    public AjaxResult add(@RequestBody Location location)
    {
        location.setCreateTime(new Date());
        location.setCreateName("张三");
        return toAjax(locationService.insertLocation(location));
    }

    /**
     * 修改库位管理
     */
    @RequiresPermissions("location:location:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody Location location)
    {
        location.setCreateTime(new Date());
        location.setCreateName("张三");
        return toAjax(locationService.updateLocation(location));
    }

    /**
     * 删除库位管理
     */
    @RequiresPermissions("location:location:remove")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(locationService.deleteLocationByIds(ids));
    }
}
